#!/bin/sh

#
# Test out reads and writes beyond the fat_cache size. For this
# to work, the file size needs to be larger than the cache.
# Currently the cache is 12 MiB (See fwup_apply.c) and the file
# is 15 MB.
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"
create_15M_file

cat >$CONFIG <<EOF
define(BOOT_PART_OFFSET, 63)
define(BOOT_PART_COUNT, 77238)

file-resource 15M.bin {
	host-path = "${TESTFILE_15M}"
}

mbr mbr-a {
    partition 0 {
        block-offset = \${BOOT_PART_OFFSET}
        block-count = \${BOOT_PART_COUNT}
        type = 0xc # FAT32
        boot = true
    }
}
task complete {
	on-init {
                mbr_write(mbr-a)
                fat_mkfs(\${BOOT_PART_OFFSET}, \${BOOT_PART_COUNT})
        }
        on-resource 15M.bin {
                fat_write(\${BOOT_PART_OFFSET}, "0.bin")
                fat_cp(\${BOOT_PART_OFFSET}, "0.bin", "1.bin")
        }
        on-finish {
        }
}
EOF

# Create the firmware file, then "burn it"
$FWUP_CREATE -c -f $CONFIG -o $FWFILE
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete

EXPECTED_OUTPUT=$WORK/expected.out
ACTUAL_OUTPUT=$WORK/actual.out

cat >$EXPECTED_OUTPUT << EOF
 Volume in drive : has no label
 Volume Serial Number is 0022-2DB6
Directory for ::/

0        bin  15000000 1980-01-01   0:00
1        bin  15000000 1980-01-01   0:00
        2 files          30 000 000 bytes
                                  8 910 336 bytes free

EOF

# Check that the directory looks right
mdir -i $WORK/fwup.img@@32256 > $ACTUAL_OUTPUT
diff -w $EXPECTED_OUTPUT $ACTUAL_OUTPUT

# Check the contents of the file
for i in 0 1; do
    mcopy -n -i $WORK/fwup.img@@32256 ::/${i}.bin $WORK/actual.${i}.bin
    diff $TESTFILE_15M $WORK/actual.${i}.bin
done

# Check the FAT file format using fsck
dd if=$WORK/fwup.img skip=63 of=$WORK/vfat.img
$FSCK_FAT $WORK/vfat.img

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
